//
//  UserInsuranceManager.m
//  MemberProject
//
//  Created by vincent luo on 15/7/27.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "UserInsuranceManager.h"

//文件夹相对路径
#define kFileDirectory     @"/Log"
#define kLogDirectory      @"/LoginUserInsurance"

@implementation UserInsuranceManager
+ (UserInsuranceManager *)sharedManager
{
    static UserInsuranceManager *sharedUserInsuranceManagerInstance = nil;
    
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedUserInsuranceManagerInstance = [[self alloc] init];
        [sharedUserInsuranceManagerInstance createDirectoty];
        //        [sharedUserInfoManagerInstance refreshUserInfo];
    });
    return sharedUserInsuranceManagerInstance;
}
/*
 存储日志路径
 */
- (NSString *)getFilePath:(NSString *)fileName
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    return [[array objectAtIndex:0] stringByAppendingString:fileName];
}


#pragma mark 创建文件目录
- (BOOL)createDirectoty
{
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *logDirectory = [self getFilePath:kFileDirectory];
    
    return [fileManager createDirectoryAtPath:logDirectory
                  withIntermediateDirectories:YES
                                   attributes:nil
                                        error:nil];
}

#pragma mark 写入本地日志
-(void)writeLocalLog:(NSDictionary *)dic
{
    [self removeUserFile];
    NSMutableDictionary *userDic = [NSMutableDictionary dictionaryWithDictionary:dic];
    for (NSString *key in userDic.allKeys) {
        NSObject *object = userDic[key];
        if ([object isKindOfClass:[NSNull class]]) {
            [userDic setValue:@"" forKey:key];
        }
    }
    
    
    NSString *path = [self getFilePath:kFileDirectory];
    NSString *filePath =[NSString stringWithFormat:@"%@%@",path,kLogDirectory];
    
    [userDic writeToFile:filePath atomically:YES];
    
}

#pragma mark 获取本地日志
-(NSDictionary *)getLocalLog
{
    NSString *path = [[UserInsuranceManager sharedManager] getFilePath:kFileDirectory];
    NSString *localPath = [NSString stringWithFormat:@"%@%@",path,kLogDirectory];
    //    [self removeUserFile];
    
    return [NSDictionary dictionaryWithContentsOfFile:localPath];
}

#pragma mark 删除文件
-(void)removeUserFile
{
    // 文件存放目录
    NSString *filePath = [self getFilePath:[NSString stringWithFormat:@"%@%@",kFileDirectory,kLogDirectory]];
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    BOOL bRet = [fileMgr fileExistsAtPath:filePath];
    if (bRet) {
        //
        NSError *err;
        [fileMgr removeItemAtPath:filePath error:&err];
    }
}

-(void)setupUserInsurance:(CSVoidBlock)resultBlock
{
    NSDictionary *dic =  [[UserInsuranceManager sharedManager]getLocalLog];
    if (!dic) {
        [[BuaatVolunteerServerSDK sharedInstance]requestVolunteerGetVolunteerInsuranceWithVolunteerCode:[UserInfoManager sharedManager].modelMser.volunteerCode success:^(NSHTTPURLResponse *response, id responseObject) {
            NSDictionary *resultDic=responseObject[@"result"];
            if(![resultDic isKindOfClass:[NSNull class]]){
                
                ModelInsurance *insurance = [ModelInsurance getInsuranceForDic:resultDic];
                NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
                NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
                [formatter setTimeZone:tz];
                [formatter setDateFormat:@"yyyyMMdd"];
                
                
                NSDate *startDate = [formatter dateFromString:insurance.periodStart];
                NSDate *endDate = [formatter dateFromString:insurance.periodEnd];
                
                NSDateFormatter *newFormat = [[NSDateFormatter alloc]init];
                [newFormat setDateFormat:@"yyyy-MM-dd"];
                
                insurance.periodStart = [newFormat stringFromDate:startDate];
                insurance.periodEnd = [newFormat stringFromDate:endDate];
                
                insurance.beginDate = startDate;
                
                insurance.endDate = endDate;
                
                
                [[UserInsuranceManager sharedManager]writeLocalLog:resultDic];
                
                
                [UserInsuranceManager sharedManager].userInsurance = insurance;
                
                resultBlock();
            }
        } failure:^(NSHTTPURLResponse *response, NSError *error) {
        }];
    }else
    {
        ModelInsurance *insurance = [ModelInsurance getInsuranceForDic:dic];
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
        [formatter setTimeZone:tz];
        [formatter setDateFormat:@"yyyyMMdd"];
        
        
        NSDate *startDate = [formatter dateFromString:insurance.periodStart];
        NSDate *endDate = [formatter dateFromString:insurance.periodEnd];
        
        NSDateFormatter *newFormat = [[NSDateFormatter alloc]init];
        [newFormat setDateFormat:@"yyyy-MM-dd"];
        
        insurance.periodStart = [newFormat stringFromDate:startDate];
        insurance.periodEnd = [newFormat stringFromDate:endDate];
        
        insurance.beginDate = startDate;
        
        insurance.endDate = endDate;
        
        [UserInsuranceManager sharedManager].userInsurance = insurance;
        resultBlock();
    }
    
    
}













@end
